import { Application, FrontendSession, RemoterClass } from "pinus";
import aviator, { Aviator, STATUS } from "../../../games/aviator";
import controlConfig, { ControlConfig } from "../../../games/aviator/controlConfig";
import gameConfig, { GameConfig } from "../../../games/aviator/gameConfig";

export default function (app: Application) {
    return new Remote(app)
}

declare global {
    interface UserRpc {
        aviator: {
            remote: RemoterClass<FrontendSession, Remote>
        }
    }
}

export class Remote {
    private _aviator: Aviator
    private _gameConfig: GameConfig
    private _controlConfig: ControlConfig
    constructor(private app: Application) {
        this._aviator = aviator(app)
        this._gameConfig = gameConfig(app)
        this._controlConfig = controlConfig(app)
    }
    // 查询玩家是否存在
    public async haveUserBychannelId(values: any): Promise<Number> {
        //查询渠道id是否存在，存在返回id，不存在返回-1
        let uid = await this._aviator.haveUserBychannelId(values.channel_id)
        //如果不存在用户  就创建
        if (uid == -1) {
            uid =  await this._aviator.createUserReturnId(values)
        }
        return uid
    }
    // 玩家掉线
    public async getGameClientUrl() {
        return this._aviator.getGameClientUrl()
    }
    // 玩家掉线
    public async offline(userId: number) {
        return this._aviator.offline(userId)
    }
    // 停止游戏
    public stopGame() {
        return this._aviator.stop()
    }
    // 踢出所有玩家
    public async kickAll() {
        return await this._aviator.kickAll()
    }
    // 机器人下注
    public robotBet(robotBetInfo: any) {
        this._aviator.robotBet(robotBetInfo)
    }
    // 加载游戏配置
    public async reload() {
        return await this._gameConfig.reload()
    }
    // 加载控制配置
    public async reloadControl() {
        return await this._controlConfig.reload()
    }
    // 增加观察者
    public async addObserver(observerId: number): Promise<boolean> {
        return await this._aviator.addObserver(observerId)
    }
    // 移除观察者
    public async removeObserver(observerId: number) {
        return this._aviator.removeObserver(observerId)
    }

    public async sendToClient() {
        await this._aviator.sendToAll("config", {})
    }
    public async forceEnd(roundId: number, operatorId: number): Promise<number> {
        if (roundId != this._aviator.roundId) {
            return 4
        }
        if (this._aviator.status !== STATUS.GAME) {
            return 5
        }
        if (!await this._aviator.forceEnd(roundId, operatorId)) {
            return 6
        }
        return 0
    }

    public async increaseCoin(userId: number, coin: number): Promise<number> {
        if (coin <= 0) {
            return 1
        }
        if (!this._aviator.getUserInfo(userId)) {
            return 4
        }
        if (this._aviator.betTimes(userId) > 0) {
            return 5
        }
        if (!await this._aviator.increaseCoin(userId, coin)) {
            return 6
        }
        return 0
    }

    public async decreaseCoin(userId: number, coin: number): Promise<number> {
        if (coin < 0) {
            return 1
        }
        if (!this._aviator.getUserInfo(userId)) {
            return 4
        }
        if (this._aviator.betTimes(userId) > 0) {
            return 5
        }
        if (!await this._aviator.decreaseCoin(userId, coin)) {
            return 6
        }
        return 0
    }

    public async reloadControlSetting() {
        return await this._gameConfig.reloadControlSetting()
    }
}
